package org.vaadinpa.logic;

import org.vaadinpa.data.Planet;

public abstract class Command {

	public boolean executed = false;
	public final Planet planet;
	public final int issuedAt;

	/**
	 * Parameter used for those commands that are not removeable (
	 * {@link #isRemoveable()} returns false).
	 */
	public boolean cancelNextTick = false;

	public Command(Planet planet) {
		this.planet = planet;
		issuedAt = Ticker.currentTick;
	}

	public abstract void execute();

	public boolean canBeCanceled() {
		return true;
	}

	/**
	 * Tries to cancel this command immediately. If cancellation is not
	 * possible, it throws {@link UnsupportedOperationException}, and
	 * {@link #cancelNextTick} is set to <code>true</code>
	 * <p>
	 * May only be called from {@link CommandHandler}
	 */
	public abstract void cancel() throws UnsupportedOperationException;

	public abstract String getName();

	public abstract String getDescription();

	public abstract void serialize(StringBuilder sb);

	public void serializeBase(StringBuilder sb) {
		sb.append("<" + this.getClass().getSimpleName() + " excecuted=\"" + executed + "\" issued=\"" + issuedAt
				+ "\" cancel=\"" + cancelNextTick + "\">");
		sb.append("<planet x=\"" + planet.coords.x + "\" y=\"" + planet.coords.y + "\"/>");

	}
}
